package com.zhuss.product.processor;

import java.util.Date;
import java.util.List;

import com.zhuss.product.processor.cache.TrainCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.train.CreateTrainBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.exception.DzcxException;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.Train;
import com.zhuss.util.JsonUtil;


/**
 * @description: 车次创建
 * @author: zhushanshuo
 * @create: Mon Jan 04 08:55:12 CST 2021
 **/
@Component("trainCreate")
public class TrainCreate extends AbstractProcessor<Object, CreateTrainBo> implements Processor<Object, CreateTrainBo> {

    @Autowired
    private TrainDao trainDao;

    @Autowired
	@Qualifier("trainRead")
	private TrainRead trainRead;

	@Autowired
	@Qualifier("trainCache")
    private TrainCache trainCache;


    /**
     * 校验
     */
    @Override
    public void check(CreateTrainBo bo) throws DzcxException {

    	String name = bo.getName();
    	if (name == null || "".equals(name.trim())) {
    		// throw new DzcxException(ExceptionCode.车次name为空);
    	}

    	List<Train> list = trainRead.list(name);
    	if (!list.isEmpty()) {
    		// throw new DzcxException(ExceptionCode.车次name已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreateTrainBo param) {
    	Train record = new Train();
    	record.setLineStrategyId(param.getLineStrategyId());
    	record.setLineId(param.getLineId());
    	record.setLineName(param.getLineName());
    	record.setLineType(param.getLineType());
    	record.setRideType(param.getRideType());
    	record.setCarId(param.getCarId());
    	record.setCarPlateNo(param.getCarPlateNo());
    	record.setDriverId(param.getDriverId());
    	record.setDriverName(param.getDriverName());
    	record.setDriverPhone(param.getDriverPhone());
    	record.setName(param.getName());
    	record.setDate(param.getDate());
    	record.setStartTime(param.getStartTime());
    	record.setEndTime(param.getEndTime());
    	record.setStartPointName(param.getStartPointName());
    	record.setEndPointName(param.getEndPointName());
    	record.setStatus(param.getStatus());
    	record.setPeopleQuantity(param.getPeopleQuantity());
    	record.setTotal(param.getTotal());
    	record.setUsed(param.getUsed());
    	record.setPrice(param.getPrice());
    	record.setSort(param.getSort());


    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	trainDao.create(record);
    	param.setTargetId(record.getTrainId());

		trainCache.clear(record.getTrainId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(CreateTrainBo param) throws DzcxException {
		OpLog record = new OpLog();
//    	record.setMoudle(MoudleEnum.默认模块请修改.key());
//    	record.setAction(LogActionEnum.车次创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
